package com.ilop.sthome.ui.adapter.room;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.greenDao.RoomBean;
import com.ilop.sthome.utils.ConfigureUtil;
import com.siterwell.familywellplus.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @Date on 2020-02-11.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：房间管理页面
 */
public class RoomManageAdapter extends RecyclerView.Adapter<RoomManageAdapter.ItemHolder> {

    private Context mContext;
    private boolean isBindRoom;
    private List<RoomBean> mList;
    private int mPosition = -1;

    public RoomManageAdapter(Context mContext, boolean isBindRoom) {
        this.mContext = mContext;
        this.isBindRoom = isBindRoom;
    }

    public void setList(List<RoomBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    public void setPosition(int mPosition) {
        this.mPosition = mPosition;
        notifyDataSetChanged();
    }

    public int getPosition() {
        return mPosition;
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.item_room_manage, viewGroup, false);
        return new ItemHolder(mView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        RoomBean roomBean = mList.get(i);
        if (isBindRoom){
            itemHolder.mRight.setVisibility(View.GONE);
            itemHolder.mSelect.setVisibility(View.VISIBLE);
        }
        if (mPosition !=-1 ) {
            if (mPosition == i) {
                itemHolder.mSelect.setImageResource(R.mipmap.selected_48);
            }else {
                itemHolder.mSelect.setImageResource(R.mipmap.unselected_48);
            }
        }else {
            itemHolder.mSelect.setImageResource(R.mipmap.unselected_48);
        }
        if (!TextUtils.isEmpty(roomBean.getNickname())){
            itemHolder.mName.setText(roomBean.getNickname());
        }else {
            itemHolder.mName.setText(ConfigureUtil.roomName(mContext, roomBean.getRid()));
        }
        itemHolder.mSelect.setOnClickListener(view -> LiveDataBus.get().with(CommonId.ROOM_MANAGER).setValue(i));
        itemHolder.itemView.setOnClickListener(v -> LiveDataBus.get().with(CommonId.ROOM_MANAGER).setValue(i));
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_room_name)
        TextView mName;
        @BindView(R.id.item_room_select)
        ImageView mSelect;
        @BindView(R.id.item_room_right)
        ImageView mRight;
        ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
